﻿using System;
using HT.Framework;
using UnityEngine;

namespace TG
{
    public class PoolItemInitManager : HTBehaviour
    {
        public GetPointTextUI getScorePrefab;
        public GetPointTextUI lossScorePrefab;

        public GameObject sheep;

        public GameObject dangerArea;
        
        public GameObject wolf;

        public GameObject transportCase;

        public GameObject treasureBox;

        public GameObject pumpkin;

        public GameObject peach;

        public GameObject cabbage;

        public GameObject greenGrass;

        public Transform pool;

        private static PoolItemInitManager instance;

        public static PoolItemInitManager Instance
        {
            get
            {
                if (instance == null)
                {
                    Debug.LogError($"FlyScoreManager 未初始化！！！");
                }

                return instance;
            }
        }

        protected override void Awake()
        {
            base.Awake();
            instance = this;
            DontDestroyOnLoad(this);

            pool = new GameObject("Pool").transform;
            pool.position = Vector3.zero;
            pool.rotation = Quaternion.identity;
        }

        private void Start()
        {
            Main.m_ObjectPool.RegisterSpawnPool("AddScore", getScorePrefab.gameObject);
            Main.m_ObjectPool.RegisterSpawnPool("LossScore", lossScorePrefab.gameObject);

            Main.m_ObjectPool.RegisterSpawnPool("Wolf", wolf, SetParent);

            Main.m_ObjectPool.RegisterSpawnPool("TransportCase", transportCase, SetParent);

            Main.m_ObjectPool.RegisterSpawnPool("TreasureBox", treasureBox, SetParent);

            Main.m_ObjectPool.RegisterSpawnPool("Pumpkin", pumpkin, SetParent);

            Main.m_ObjectPool.RegisterSpawnPool("Peach", peach, SetParent);

            Main.m_ObjectPool.RegisterSpawnPool("Cabbage", cabbage, SetParent);

            Main.m_ObjectPool.RegisterSpawnPool("GreenGrass", greenGrass, SetParent);
            
            Main.m_ObjectPool.RegisterSpawnPool("DangerArea", dangerArea, SetParent);
        }


        private void SetParent(GameObject obj)
        {
            obj.transform.parent = pool;
        }

        // private GetPointTextUI CreateScorePrefab(int _score, Vector3 _pos)
        // {
        //     GetPointTextUI score = _score > 0
        //         ? Main.m_ObjectPool.Spawn("AddScore").GetComponent<GetPointTextUI>()
        //         : Main.m_ObjectPool.Spawn("LossScore").GetComponent<GetPointTextUI>();
        //
        //     var ui= Main.m_UI.GetOpenedUI<GameplayPanel>().UIEntity;
        //     score.transform.SetParent(ui.transform, false);
        //     score.transform.position = _pos;
        //
        //     string sign = (_score > 0) ? "+" : string.Empty;
        //     score.SetText(sign + _score);
        //
        //     return score;
        // }
        //
        // public GetPointTextUI GetOrLossScore(int _score, Vector3 _position, bool _isScreenPos = false)
        // {
        //     if (GameStateManager.Instance.GameState != GameStates.Playing)
        //         return null;
        //
        //     GameStart.Instance.GameData.Score += _score;
        //
        //     var scnPos = (_isScreenPos) ? _position : GameManager.Instance.camera3D.WorldToScreenPoint(_position);
        //     return CreateScorePrefab(_score, scnPos);
        // }

        protected override void OnDestroy()
        {
            Main.m_ObjectPool.ClearAll();
            base.OnDestroy();
        }
    }
}